// pages/myclass/myclass.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getcourse: app.data.url +'/zhongxue/maoapi/getcourse',
    date: app.data.url +"/uploads/zhongxue/date.png",
    time: app.data.url +"/uploads/zhongxue/xdate.png",
    shanchu: app.data.url + "/uploads/zhongxue/delete.png",
    deletesigup: app.data.url + "/zhongxue/maoapi/deletesigup",
    class:[
      { title: "【2019暑】四年级趣味语文入门班", src:app.data.url +"/uploads/zhongxue/kteacher.png",name:"张域名",kemu:"语文",banji:"初三二班",number:"30",date:"06月12日-12月30日",time:"周日16：00-18：30"},
      { title: "【2019暑】四年级趣味语文入门班", src: app.data.url +"/uploads/zhongxue/kteacher.png", name: "张域名", kemu: "语文", banji: "初三二班", number: "30", date: "06月12日-12月30日", time: "周日16：00-18：30" }
       ],
       shuju:[],
    student_id:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var students_id = options.student_id;
    this.setData({ student_id: students_id});
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getcourse,
      data: { token: user.token, students_id: students_id},
      method: 'POST',
      success: function(res) {
        var data = res.data;
        console.log(data);
        if(data.code==200){
          that.setData({shuju:data.data});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  shanchus:function(e){
console.log(e)
    var course_id = e.currentTarget.dataset.id;
    var student_id = this.data.student_id;
    var user = wx.getStorageSync('user');
    var that = this;
    console.log(course_id);
    console.log(student_id);
    wx.showModal({
      title: '提示',
      content: '你确定要取消报名吗？',
      success: function(res) {
        if (res.confirm){
          wx.request({
            url: that.data.deletesigup,
            data: { course_id: course_id, student_id: student_id, token: user.token },
            header: {},
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: function (ress) {
              var data = ress.data;
              // console.log(data)
              // return
              if(data.code==200){
                wx.showToast({
                  title: '取消成功',
                  icon:'success',
                  mask:true,
                  duration: app.data.quanbushijian
                })
                var shuju = that.data.shuju;
                var newshuju = [];
                ea.each(shuju,function(i,v){
                  if (v.id !== course_id){
                    newshuju.push(v);
                  }
                })
                that.setData({ shuju:newshuju});

              }else{
                wx.showToast({
                  title: data.msg,
                  icon: 'none',
                  duration: app.data.quanbushijian,
                  mask: true,
                })
              }
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})